package com.audaque.springboot.foshanupload.web.lic.listener;

import com.audaque.springboot.foshanupload.web.lic.service.LicenseVerifyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

/**
 * 在项目启动时安装证书
 *
 * @author huangchen@deepglint.com
 * @desc
 * @date 2019/3/14 13:18
 */
@Component
@Slf4j
public class ApplicationListener implements org.springframework.context.ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private LicenseVerifyService licenseVerifyService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        //root application context 没有parent
        ApplicationContext context = event.getApplicationContext().getParent();
        if (context == null) {
            boolean result1 = licenseVerifyService.install();
            boolean result = licenseVerifyService.verify();

            if (!result) {
                log.error("授权文件安装校验失败！");
            }
        }
    }
}
